﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = fillMenu();
    }
    public string fillMenu()
    {
        string ds = "";
        int parentID = 0;
        ds += "<ul id=\"nav\">";
        MenuDataContext mn = new MenuDataContext();
        IEnumerable<TMenu> level1 = from m in mn.TMenus
                                    where m.levelID == 1
                                    select m;
        foreach (TMenu t1 in level1)
        {
            ds += "<li><a href=\"" + t1.url + "\">" + t1.itemText + "</a>";
            parentID = Int32.Parse(t1.itemID.ToString());
            MenuDataContext mn2 = new MenuDataContext();
            IEnumerable<TMenu> level2 = from m in mn2.TMenus
                                        where (m.levelID == 2 && m.parentID == parentID)
                                        select m;
            if (level2.Count() > 0)
            {
                ds += "<ul>";
                foreach (TMenu t2 in level2)
                {
                    ds += "<li>";
                    parentID = Int32.Parse(t2.itemID.ToString());
                    MenuDataContext mn3 = new MenuDataContext();
                    IEnumerable<TMenu> level3 = from m in mn3.TMenus
                                                where (m.levelID == 3 && m.parentID == parentID)
                                                select m;
                    if (level3.Count() > 0)
                    {
                        ds += "<a href=\"" + t2.url + "\">" + t2.itemText + " &#187;</a>";
                        ds += "<ul>";
                        foreach (TMenu t3 in level3)
                        {
                            ds += "<li>";
                            parentID = Int32.Parse(t3.itemID.ToString());
                            MenuDataContext mn4 = new MenuDataContext();
                            IEnumerable<TMenu> level4 = from m in mn4.TMenus
                                                        where (m.levelID == 4 && m.parentID == parentID)
                                                        select m;
                            if (level4.Count() > 0)
                            {
                                ds += "<a href=\"" + t3.url + "\">" + t3.itemText + " &#187;</a>";
                                ds += "<ul>";
                                foreach (TMenu t4 in level4)
                                {
                                    ds += "<li><a href=\"" + t4.url + "\">" + t4.itemText + "</a></li>";
                                }
                                ds += "</ul>";
                            }
                            else
                            {
                                ds += "<a href=\"" + t3.url + "\">" + t3.itemText + "</a>";
                            }
                            ds += "</li>";
                        }
                        ds += "</ul>";
                    }
                    else
                    {
                        ds += "<a href=\"" + t2.url + "\">" + t2.itemText + "</a>";
                    }
                    ds += "</li>";
                }
                ds += "</ul>";
            }
            ds += "</li>";
        }
        ds += "</ul>";
        return ds;

    }
}